Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 765)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 13.04512 13.04071 13.03636 13.03205 13.02780 13.02359 13.01943 13.01531
## [9] 13.01124 13.00721 13.00323 12.99928 12.99538 12.99152 12.98769 12.98391
## [17] 12.98015 12.97644 12.97276 12.96911 12.96550 12.96191 12.95836 12.95484
## [25] 12.95134 12.94788 12.94444 12.94102 12.93764 12.93427 12.93093 12.92760
## [33] 12.92430 12.92102 12.91776 12.91452 12.91129 12.90808 12.90488 12.90169
## [41] 12.89852 12.89536 12.89221 12.88907 12.88592 12.88277 12.87962 12.87647
## [49] 12.87334 12.87022 12.86711 12.86403 12.86096 12.85793 12.85492 12.85194
## [57] 12.84899 12.84609 12.84323 12.84041 12.83764 12.83492 12.83225 12.82965
## [65] 12.82710 12.82462 12.82221 12.81987 12.81760 12.81541 12.81330 12.81127
## [73] 12.80933 12.80748 12.80572 12.80406 12.80250 12.80105 12.79970 12.79846
## [81] 12.79733 12.79632 12.79543 12.79466 12.79402 12.79339 12.79265 12.79183
## [89] 12.79091 12.78993 12.78887 12.78775 12.78658 12.78537 12.78413 12.78285
## [97] 12.78156 12.78027 12.77896 12.77767 12.77639 12.77514 12.77392 12.77274
## [105] 12.77161 12.77053 12.76952 12.76859 12.76774 12.76698 12.76632 12.76576
## [113] 12.76533 12.76501 12.76483 12.76480 12.76491 12.76518 12.76561 12.76622
## [121] 12.76702 12.76800 12.76919 12.77058 12.77219 12.77403 12.77609 12.77841
## [129] 12.78163 12.78634 12.79239 12.79964 12.80793 12.81713 12.82709 12.83766
## [137] 12.84871 12.86008 12.87163 12.88321 12.89468 12.90590 12.91672 12.92699
## [145] 12.93657 12.94531 12.95308 12.95972 12.96508 12.97104 12.97936 12.98980
## [153] 13.00211 13.01603 13.03132 13.04771 13.06497 13.08284 13.10107 13.11940
## [161] 13.13758 13.15537 13.17251 13.18875 13.20384 13.21752 13.22955 13.23968
## [169] 13.24765 13.25320 13.25773 13.26274 13.26818 13.27399 13.28012 13.28651
## [177] 13.29311 13.29986 13.30670 13.31358 13.32045 13.32724 13.33391 13.34040
## [185] 13.34665 13.35260 13.35821 13.36341 13.36815 13.37238 13.37604 13.37907
## [193] 13.38142 13.38304 13.38386 13.38383 13.38291 13.38102 13.37812 13.37415
## [201] 13.36905 13.36277 13.35526 13.34646 13.33550 13.32173 13.30543 13.28685
## [209] 13.26626 13.24394 13.22014 13.19513 13.16917 13.14255 13.11551 13.08833
## [217] 13.06127 13.03460 13.00858 12.98349 12.95958 12.93713 12.91640 12.89765
## [225] 12.87830 12.85578 12.83042 12.80257 12.77253 12.74066 12.70727 12.67269
## [233] 12.63726 12.60130 12.56515 12.52914 12.49359 12.45883 12.42520 12.39302
## [241] 12.36262 12.33434 12.30851 12.28544 12.26548 12.24744 12.22991 12.21285
## [249] 12.19626 12.18010 12.16437 12.14902 12.13406 12.11944 12.10516 12.09118
## [257] 12.07750 12.06408 12.05090 12.03795 12.02520 12.01264 12.00023 11.98795
## [265] 11.97580 11.96373 11.95283 11.94402 11.93710 11.93189 11.92816 11.92574
## [273] 11.92440 11.92395 11.92420 11.92493 11.92595 11.92706 11.92805 11.92873
## [281] 11.92890 11.92834 11.92687 11.92427 11.92036 11.91492 11.90776 11.89986
## [289] 11.89231 11.88506 11.87809 11.87135 11.86480 11.85842 11.85215 11.84596
## [297] 11.83982 11.83369 11.82752 11.82128 11.81493 11.80844 11.80176 11.79487
## [305] 11.78771 11.78025 11.77246 11.76430 11.75486 11.74344 11.73027 11.71556
## [313] 11.69955 11.68246 11.66452 11.64596 11.62700 11.60787 11.58881 11.57002
## [321] 11.55175 11.53422 11.51766 11.50229 11.48834 11.47604 11.46562 11.45730
## [329] 11.45130 11.44564 11.43829 11.42949 11.41947 11.40846 11.39669 11.38440
## [337] 11.37180 11.35914 11.34665 11.33455 11.32307 11.31245 11.30291 11.29469
## [345] 11.28802 11.28312 11.28024 11.27959 11.28141 11.28593 11.29302 11.30232
## [353] 11.31366 11.32686 11.34178 11.35826 11.37612 11.39520 11.41535 11.43641
## [361] 11.45820 11.48057 11.50336 11.52641 11.54955 11.57261 11.59545 11.61790
## [369] 11.63979 11.66096 11.68125 11.70051 11.72123 11.74581 11.77385 11.80497
## [377] 11.83879 11.87494 11.91303 11.95269 11.99352 12.03515 12.07721 12.11930
## [385] 12.16105 12.20208 12.24201 12.28046 12.31704 12.35137 12.38309 12.41179
## [393] 12.43711 12.45866 12.47910 12.50118 12.52467 12.54935 12.57498 12.60135
## [401] 12.62822 12.65538 12.68258 12.70961 12.73624 12.76225 12.78740 12.81147
## [409] 12.83424 12.85547 12.87494 12.89242 12.90769 12.92052 12.93069 12.93836
## [417] 12.94397 12.94768 12.94963 12.94998 12.94890 12.94652 12.94301 12.93852
## [425] 12.93321 12.92723 12.92073 12.91387 12.90680 12.89969 12.89267 12.88592
## [433] 12.87957 12.87380 12.86874 12.86456 12.85937 12.85136 12.84079 12.82796
## [441] 12.81313 12.79658 12.77857 12.75940 12.73932 12.71862 12.69757 12.67644
## [449] 12.65551 12.63506 12.61535 12.59667 12.57928 12.56346 12.54949 12.53765
## [457] 12.52819 12.51902 12.50798 12.49528 12.48112 12.46573 12.44930 12.43204
## [465] 12.41417 12.39589 12.37742 12.35896 12.34073 12.32293 12.30577 12.28946
## [473] 12.27421 12.26023 12.24773 12.23692 12.22801 12.22121 12.21550 12.20978
## [481] 12.20408 12.19845 12.19293 12.18757 12.18241 12.17751 12.17289 12.16861
## [489] 12.16471 12.16123 12.15823 12.15573 12.15380 12.15247 12.15179 12.15180
## [497] 12.15255 12.15408 12.15644 12.16043 12.16671 12.17510 12.18542 12.19750
## [505] 12.21114 12.22617 12.24242 12.25969 12.27781 12.29659 12.31586 12.33544
## [513] 12.35514 12.37479 12.39420 12.41320 12.43160 12.44922 12.46588 12.48141
## [521] 12.49561 12.50832 12.51935 12.52852 12.53564 12.54242 12.55057 12.55996
## [529] 12.57045 12.58192 12.59421 12.60721 12.62077 12.63477 12.64905 12.66350
## [537] 12.67797 12.69234 12.70646 12.72020 12.73342 12.74600 12.75779 12.76867
## [545] 12.77849 12.78712 12.79443 12.80028 12.80454 12.80706 12.80773 12.80640
## [553] 12.80294 12.79741 12.79008 12.78110 12.77061 12.75877 12.74572 12.73161
## [561] 12.71660 12.70082 12.68444 12.66759 12.65044 12.63312 12.61579 12.59859
## [569] 12.58168 12.56520 12.54931 12.53415 12.51987 12.50662 12.49210 12.47415
## [577] 12.45313 12.42940 12.40331 12.37522 12.34549 12.31448 12.28254 12.25003
## [585] 12.21731 12.18474 12.15267 12.12146 12.09147 12.06306 12.03658 12.01240
## [593] 11.99086 11.97233 11.95717 11.94258 11.92573 11.90688 11.88631 11.86429
## [601] 11.84110 11.81701 11.79230 11.76724 11.74210 11.71716 11.69269 11.66897
## [609] 11.64627 11.62486 11.60502 11.58702 11.57114 11.55765 11.54682 11.53893
## [617] 11.53353 11.52992 11.52797 11.52757 11.52859 11.53090 11.53439 11.53894
## [625] 11.54442 11.55071 11.55769 11.56524 11.57323 11.58155 11.59006 11.59866
## [633] 11.60722 11.61562 11.62372 11.63143 11.63860 11.64679 11.65747 11.67041
## [641] 11.68539 11.70216 11.72052 11.74021 11.76102 11.78272 11.80507 11.82785
## [649] 11.85083 11.87378 11.89646 11.91865 11.94013 11.96065 11.98000 11.99794
## [657] 12.01424 12.02868 12.04264 12.05761 12.07349 12.09017 12.10756 12.12556
## [665] 12.14405 12.16294 12.18214 12.20153 12.22101 12.24049 12.25987 12.27904
## [673] 12.29789 12.31634 12.33427 12.35159 12.36820 12.38399 12.39886 12.41271
## [681] 12.42544 12.43763 12.44989 12.46220 12.47452 12.48683 12.49908 12.51125
## [689] 12.52331 12.53522 12.54696 12.55848 12.56977 12.58078 12.59149 12.60185
## [697] 12.61185 12.62145 12.63062 12.63932 12.64753 12.65521 12.66255 12.66974
## [705] 12.67678 12.68366 12.69037 12.69688 12.70320 12.70931 12.71520 12.72085
## [713] 12.72626 12.73141 12.73629 12.74090 12.74521 12.74923 12.75292 12.75630
## [721] 12.75933 12.76202 12.76435 12.76637 12.76816 12.76971 12.77103 12.77209
## [729] 12.77291 12.77348 12.77380 12.77387 12.77368 12.77322 12.77251 12.77153
## [737] 12.77029 12.76877 12.76698 12.76492 12.76257 12.75995 12.75704 12.75385
## [745] 12.75036 12.74657 12.74248 12.73809 12.73341 12.72843 12.72316 12.71761
## [753] 12.71178 12.70566 12.69926 12.69259 12.68565 12.67844 12.67095 12.66321
## [761] 12.65520 12.64693 12.63840 12.62962 12.62059
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 765)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.63119 12.62628 12.62147 12.61675 12.61212 12.60758 12.60313 12.59877
## [9] 12.59450 12.59032 12.58623 12.58222 12.57830 12.57447 12.57072 12.56706
## [17] 12.56349 12.55999 12.55658 12.55325 12.55001 12.54684 12.54376 12.54076
## [25] 12.53783 12.53499 12.53222 12.52953 12.52692 12.52439 12.52193 12.51954
## [33] 12.51723 12.51500 12.51284 12.51075 12.50873 12.50679 12.50491 12.50311
## [41] 12.50138 12.49971 12.49811 12.49658 12.49512 12.49372 12.49238 12.49112
## [49] 12.48992 12.48880 12.48775 12.48678 12.48589 12.48507 12.48434 12.48369
## [57] 12.48312 12.48264 12.48225 12.48195 12.48174 12.48163 12.48160 12.48168
## [65] 12.48185 12.48213 12.48250 12.48298 12.48357 12.48426 12.48507 12.48598
## [73] 12.48700 12.48814 12.48940 12.49077 12.49227 12.49388 12.49562 12.49748
## [81] 12.49947 12.50158 12.50383 12.50621 12.50872 12.51141 12.51432 12.51743
## [89] 12.52075 12.52426 12.52796 12.53184 12.53589 12.54011 12.54448 12.54900
## [97] 12.55367 12.55847 12.56340 12.56845 12.57361 12.57888 12.58425 12.58970
## [105] 12.59525 12.60086 12.60655 12.61230 12.61810 12.62395 12.62983 12.63575
## [113] 12.64170 12.64766 12.65363 12.65960 12.66557 12.67152 12.67745 12.68336
## [121] 12.68923 12.69506 12.70084 12.70657 12.71223 12.71781 12.72375 12.73043
## [129] 12.73777 12.74569 12.75411 12.76296 12.77216 12.78164 12.79131 12.80110
## [137] 12.81093 12.82073 12.83042 12.83992 12.84915 12.85804 12.86650 12.87448
## [145] 12.88187 12.88862 12.89464 12.90174 12.91162 12.92396 12.93849 12.95491
## [153] 12.97293 12.99225 13.01259 13.03365 13.05514 13.07677 13.09825 13.11928
## [161] 13.13958 13.15884 13.17679 13.19313 13.20756 13.21980 13.22955 13.23652
## [169] 13.24228 13.24855 13.25527 13.26236 13.26975 13.27737 13.28516 13.29303
## [177] 13.30093 13.30877 13.31650 13.32403 13.33130 13.33824 13.34478 13.35085
## [185] 13.35637 13.36129 13.36552 13.36899 13.37165 13.37340 13.37420 13.37396
## [193] 13.37261 13.37009 13.36632 13.36124 13.35477 13.34685 13.33639 13.32256
## [201] 13.30569 13.28608 13.26406 13.23992 13.21400 13.18660 13.15803 13.12862
## [209] 13.09866 13.06849 13.03841 13.00873 12.97977 12.95185 12.92527 12.90036
## [217] 12.87742 12.85677 12.83873 12.82062 12.79978 12.77648 12.75104 12.72374
## [225] 12.69488 12.66475 12.63364 12.60186 12.56969 12.53742 12.50536 12.47380
## [233] 12.44302 12.41333 12.38503 12.35839 12.33372 12.31131 12.29146 12.27446
## [241] 12.25927 12.24461 12.23047 12.21682 12.20365 12.19091 12.17860 12.16668
## [249] 12.15513 12.14393 12.13305 12.12246 12.11215 12.10209 12.09225 12.08261
## [257] 12.07315 12.06384 12.05465 12.04556 12.03656 12.02903 12.02422 12.02187
## [265] 12.02171 12.02347 12.02688 12.03169 12.03762 12.04440 12.05177 12.05947
## [273] 12.06723 12.07477 12.08184 12.08817 12.09348 12.09752 12.10002 12.10071
## [281] 12.09933 12.09561 12.09115 12.08765 12.08502 12.08313 12.08187 12.08113
## [289] 12.08079 12.08074 12.08086 12.08104 12.08118 12.08115 12.08084 12.08013
## [297] 12.07892 12.07709 12.07453 12.07112 12.06675 12.06130 12.05467 12.04618
## [305] 12.03539 12.02256 12.00790 11.99165 11.97406 11.95536 11.93577 11.91555
## [313] 11.89492 11.87411 11.85338 11.83294 11.81304 11.79392 11.77580 11.75893
## [321] 11.74353 11.72985 11.71812 11.70858 11.69880 11.68641 11.67172 11.65506
## [329] 11.63675 11.61710 11.59643 11.57506 11.55332 11.53152 11.50997 11.48901
## [337] 11.46894 11.45009 11.43277 11.41731 11.40402 11.39322 11.38524 11.38038
## [345] 11.37897 11.38010 11.38258 11.38634 11.39129 11.39737 11.40451 11.41261
## [353] 11.42162 11.43145 11.44203 11.45328 11.46512 11.47749 11.49031 11.50350
## [361] 11.51698 11.53068 11.54452 11.55844 11.57234 11.58617 11.60154 11.61997
## [369] 11.64122 11.66505 11.69123 11.71951 11.74966 11.78145 11.81462 11.84895
## [377] 11.88419 11.92011 11.95646 11.99302 12.02954 12.06578 12.10150 12.13647
## [385] 12.17045 12.20320 12.23447 12.26405 12.29167 12.31711 12.34013 12.36049
## [393] 12.38041 12.40213 12.42542 12.45004 12.47578 12.50240 12.52968 12.55737
## [401] 12.58527 12.61313 12.64073 12.66784 12.69424 12.71968 12.74395 12.76682
## [409] 12.78805 12.80742 12.82470 12.83966 12.85208 12.86292 12.87329 12.88318
## [417] 12.89258 12.90147 12.90983 12.91764 12.92489 12.93157 12.93766 12.94314
## [425] 12.94799 12.95221 12.95577 12.95865 12.96085 12.96234 12.96311 12.96315
## [433] 12.96243 12.96095 12.95868 12.95561 12.95022 12.94125 12.92904 12.91394
## [441] 12.89630 12.87646 12.85477 12.83157 12.80722 12.78206 12.75643 12.73069
## [449] 12.70518 12.68024 12.65624 12.63350 12.61238 12.59322 12.57638 12.56220
## [457] 12.55102 12.54014 12.52678 12.51125 12.49380 12.47474 12.45432 12.43284
## [465] 12.41058 12.38781 12.36481 12.34186 12.31926 12.29726 12.27616 12.25623
## [473] 12.23776 12.22102 12.20629 12.19386 12.18400 12.17700 12.17129 12.16521
## [481] 12.15884 12.15227 12.14558 12.13886 12.13220 12.12568 12.11939 12.11342
## [489] 12.10785 12.10277 12.09828 12.09444 12.09136 12.08912 12.08780 12.08749
## [497] 12.08829 12.09027 12.09352 12.09878 12.10660 12.11678 12.12915 12.14349
## [505] 12.15963 12.17737 12.19652 12.21688 12.23828 12.26050 12.28337 12.30669
## [513] 12.33027 12.35392 12.37744 12.40065 12.42336 12.44536 12.46647 12.48651
## [521] 12.50526 12.52256 12.53820 12.55199 12.56374 12.57572 12.59018 12.60690
## [529] 12.62566 12.64624 12.66840 12.69193 12.71662 12.74222 12.76853 12.79531
## [537] 12.82235 12.84943 12.87631 12.90279 12.92863 12.95362 12.97752 13.00013
## [545] 13.02121 13.04054 13.05791 13.07308 13.08584 13.09596 13.10323 13.10741
## [553] 13.10829 13.10635 13.10233 13.09639 13.08868 13.07936 13.06856 13.05646
## [561] 13.04320 13.02894 13.01382 12.99801 12.98165 12.96491 12.94792 12.93085
## [569] 12.91385 12.89707 12.88067 12.86480 12.84960 12.83525 12.81929 12.79944
## [577] 12.77610 12.74966 12.72050 12.68902 12.65562 12.62068 12.58459 12.54774
## [585] 12.51054 12.47336 12.43660 12.40065 12.36590 12.33275 12.30158 12.27278
## [593] 12.24676 12.22389 12.20457 12.18541 12.16298 12.13766 12.10983 12.07986
## [601] 12.04813 12.01503 11.98091 11.94616 11.91115 11.87627 11.84188 11.80836
## [609] 11.77609 11.74545 11.71680 11.69053 11.66701 11.64662 11.62973 11.61673
## [617] 11.60616 11.59633 11.58726 11.57892 11.57131 11.56444 11.55830 11.55289
## [625] 11.54820 11.54423 11.54097 11.53843 11.53660 11.53548 11.53506 11.53534
## [633] 11.53631 11.53798 11.54034 11.54339 11.54712 11.55283 11.56164 11.57327
## [641] 11.58745 11.60390 11.62236 11.64254 11.66418 11.68700 11.71072 11.73508
## [649] 11.75979 11.78460 11.80921 11.83336 11.85678 11.87919 11.90031 11.91988
## [657] 11.93762 11.95325 11.96874 11.98610 12.00516 12.02575 12.04769 12.07082
## [665] 12.09494 12.11990 12.14552 12.17162 12.19804 12.22458 12.25110 12.27740
## [673] 12.30331 12.32866 12.35329 12.37700 12.39963 12.42101 12.44096 12.45931
## [681] 12.47588 12.49148 12.50702 12.52249 12.53786 12.55313 12.56828 12.58329
## [689] 12.59815 12.61284 12.62734 12.64165 12.65574 12.66960 12.68321 12.69657
## [697] 12.70964 12.72243 12.73490 12.74706 12.75887 12.77033 12.78152 12.79256
## [705] 12.80342 12.81412 12.82463 12.83495 12.84509 12.85502 12.86475 12.87427
## [713] 12.88357 12.89265 12.90151 12.91012 12.91850 12.92663 12.93451 12.94213
## [721] 12.94949 12.95657 12.96338 12.96994 12.97628 12.98240 12.98831 12.99400
## [729] 12.99946 13.00471 13.00974 13.01454 13.01912 13.02348 13.02762 13.03153
## [737] 13.03522 13.03868 13.04191 13.04492 13.04771 13.05026 13.05259 13.05468
## [745] 13.05655 13.05819 13.05959 13.06077 13.06173 13.06245 13.06295 13.06322
## [753] 13.06326 13.06309 13.06268 13.06205 13.06120 13.06013 13.05883 13.05731
## [761] 13.05557 13.05361 13.05143 13.04903 13.04641
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 765)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 12.04821 12.04190 12.03568 12.02955 12.02350 12.01754 12.01167 12.00587
## [9] 12.00016 11.99453 11.98898 11.98351 11.97811 11.97279 11.96755 11.96239
## [17] 11.95729 11.95227 11.94732 11.94245 11.93764 11.93290 11.92823 11.92362
## [25] 11.91909 11.91461 11.91020 11.90585 11.90157 11.89734 11.89317 11.88907
## [33] 11.88502 11.88102 11.87708 11.87320 11.86937 11.86559 11.86186 11.85819
## [41] 11.85456 11.85098 11.84745 11.84397 11.84053 11.83713 11.83378 11.83047
## [49] 11.82720 11.82398 11.82078 11.81759 11.81443 11.81130 11.80820 11.80513
## [57] 11.80211 11.79913 11.79619 11.79332 11.79050 11.78774 11.78506 11.78244
## [65] 11.77990 11.77744 11.77507 11.77279 11.77060 11.76851 11.76653 11.76465
## [73] 11.76289 11.76124 11.75971 11.75831 11.75705 11.75591 11.75492 11.75407
## [81] 11.75337 11.75282 11.75243 11.75221 11.75215 11.75226 11.75254 11.75301
## [89] 11.75366 11.75450 11.75554 11.75677 11.75809 11.75939 11.76068 11.76197
## [97] 11.76327 11.76459 11.76594 11.76734 11.76880 11.77032 11.77192 11.77360
## [105] 11.77539 11.77728 11.77930 11.78145 11.78374 11.78619 11.78880 11.79159
## [113] 11.79457 11.79774 11.80112 11.80473 11.80856 11.81264 11.81697 11.82156
## [121] 11.82643 11.83158 11.83704 11.84280 11.84887 11.85528 11.86203 11.86914
## [129] 11.87753 11.88803 11.90042 11.91452 11.93013 11.94706 11.96510 11.98407
## [137] 12.00376 12.02399 12.04455 12.06524 12.08589 12.10627 12.12621 12.14551
## [145] 12.16396 12.18138 12.19756 12.21232 12.22545 12.23965 12.25747 12.27852
## [153] 12.30243 12.32881 12.35727 12.38742 12.41890 12.45129 12.48424 12.51734
## [161] 12.55021 12.58247 12.61374 12.64362 12.67174 12.69771 12.72115 12.74166
## [169] 12.75886 12.77238 12.78392 12.79542 12.80685 12.81817 12.82933 12.84032
## [177] 12.85108 12.86157 12.87177 12.88164 12.89113 12.90020 12.90883 12.91697
## [185] 12.92459 12.93164 12.93809 12.94391 12.94905 12.95348 12.95716 12.96005
## [193] 12.96211 12.96332 12.96362 12.96298 12.96137 12.95874 12.95507 12.95030
## [201] 12.94441 12.93735 12.92909 12.91960 12.90718 12.89050 12.86997 12.84605
## [209] 12.81915 12.78971 12.75817 12.72497 12.69053 12.65530 12.61971 12.58418
## [217] 12.54916 12.51509 12.48239 12.45150 12.42285 12.39689 12.37404 12.35473
## [225] 12.33606 12.31499 12.29177 12.26667 12.23993 12.21182 12.18258 12.15249
## [233] 12.12179 12.09075 12.05961 12.02864 11.99809 11.96822 11.93928 11.91153
## [241] 11.88524 11.86065 11.83802 11.81761 11.79967 11.78349 11.76813 11.75353
## [249] 11.73962 11.72635 11.71366 11.70148 11.68976 11.67844 11.66744 11.65672
## [257] 11.64622 11.63587 11.62560 11.61537 11.60511 11.59476 11.58426 11.57355
## [265] 11.56256 11.55124 11.54076 11.53218 11.52533 11.52002 11.51608 11.51333
## [273] 11.51158 11.51065 11.51036 11.51052 11.51097 11.51150 11.51195 11.51214
## [281] 11.51187 11.51097 11.50926 11.50655 11.50267 11.49743 11.49065 11.48318
## [289] 11.47598 11.46903 11.46229 11.45576 11.44941 11.44322 11.43716 11.43121
## [297] 11.42535 11.41956 11.41381 11.40809 11.40237 11.39663 11.39084 11.38499
## [305] 11.37904 11.37299 11.36681 11.36047 11.35257 11.34193 11.32884 11.31362
## [313] 11.29657 11.27798 11.25818 11.23745 11.21611 11.19445 11.17279 11.15143
## [321] 11.13067 11.11081 11.09216 11.07503 11.05971 11.04652 11.03576 11.02772
## [329] 11.02273 11.01911 11.01510 11.01078 11.00626 11.00164 10.99702 10.99250
## [337] 10.98817 10.98414 10.98050 10.97736 10.97482 10.97297 10.97192 10.97177
## [345] 10.97261 10.97454 10.97767 10.98210 10.98791 10.99522 11.00470 11.01679
## [353] 11.03129 11.04797 11.06663 11.08704 11.10899 11.13227 11.15666 11.18194
## [361] 11.20790 11.23432 11.26098 11.28768 11.31419 11.34030 11.36579 11.39045
## [369] 11.41406 11.43640 11.45727 11.47644 11.49604 11.51817 11.54258 11.56900
## [377] 11.59719 11.62688 11.65782 11.68975 11.72242 11.75556 11.78893 11.82227
## [385] 11.85531 11.88781 11.91951 11.95014 11.97947 12.00721 12.03313 12.05697
## [393] 12.07846 12.09735 12.11534 12.13421 12.15381 12.17401 12.19467 12.21567
## [401] 12.23685 12.25809 12.27924 12.30018 12.32076 12.34085 12.36031 12.37901
## [409] 12.39681 12.41357 12.42915 12.44343 12.45626 12.46750 12.47703 12.48517
## [417] 12.49239 12.49873 12.50425 12.50900 12.51302 12.51635 12.51906 12.52119
## [425] 12.52278 12.52388 12.52455 12.52483 12.52477 12.52442 12.52383 12.52304
## [433] 12.52211 12.52108 12.52000 12.51892 12.51676 12.51252 12.50641 12.49859
## [441] 12.48927 12.47864 12.46688 12.45418 12.44074 12.42673 12.41236 12.39781
## [449] 12.38326 12.36892 12.35496 12.34159 12.32897 12.31732 12.30681 12.29763
## [457] 12.28998 12.28217 12.27252 12.26122 12.24847 12.23446 12.21939 12.20346
## [465] 12.18686 12.16978 12.15243 12.13500 12.11767 12.10066 12.08415 12.06833
## [473] 12.05342 12.03959 12.02705 12.01599 12.00661 11.99910 11.99203 11.98393
## [481] 11.97496 11.96525 11.95496 11.94423 11.93321 11.92205 11.91089 11.89987
## [489] 11.88915 11.87887 11.86917 11.86021 11.85213 11.84508 11.83920 11.83463
## [497] 11.83154 11.83006 11.83034 11.83240 11.83610 11.84132 11.84794 11.85586
## [505] 11.86496 11.87512 11.88624 11.89819 11.91087 11.92415 11.93793 11.95209
## [513] 11.96652 11.98110 11.99572 12.01026 12.02461 12.03866 12.05229 12.06540
## [521] 12.07785 12.08955 12.10037 12.11020 12.11893 12.12814 12.13935 12.15239
## [529] 12.16710 12.18329 12.20081 12.21948 12.23912 12.25957 12.28066 12.30220
## [537] 12.32404 12.34600 12.36790 12.38958 12.41087 12.43159 12.45158 12.47066
## [545] 12.48865 12.50539 12.52072 12.53444 12.54640 12.55642 12.56434 12.56997
## [553] 12.57315 12.57478 12.57587 12.57644 12.57652 12.57611 12.57524 12.57392
## [561] 12.57217 12.57002 12.56747 12.56454 12.56126 12.55764 12.55369 12.54944
## [569] 12.54491 12.54010 12.53504 12.52975 12.52424 12.51854 12.51123 12.50110
## [577] 12.48841 12.47344 12.45646 12.43775 12.41757 12.39621 12.37394 12.35102
## [585] 12.32774 12.30437 12.28117 12.25844 12.23643 12.21542 12.19569 12.17750
## [593] 12.16114 12.14688 12.13498 12.12280 12.10770 12.09000 12.07002 12.04810
## [601] 12.02455 11.99969 11.97384 11.94734 11.92049 11.89363 11.86707 11.84114
## [609] 11.81615 11.79244 11.77032 11.75012 11.73216 11.71676 11.70424 11.69493
## [617] 11.68780 11.68161 11.67629 11.67182 11.66814 11.66521 11.66298 11.66142
## [625] 11.66046 11.66008 11.66021 11.66083 11.66188 11.66332 11.66511 11.66719
## [633] 11.66953 11.67208 11.67479 11.67762 11.68053 11.68459 11.69077 11.69888
## [641] 11.70873 11.72013 11.73287 11.74677 11.76163 11.77725 11.79345 11.81002
## [649] 11.82678 11.84352 11.86006 11.87620 11.89175 11.90651 11.92029 11.93289
## [657] 11.94412 11.95378 11.96316 11.97358 11.98495 11.99716 12.01011 12.02370
## [665] 12.03782 12.05237 12.06724 12.08234 12.09756 12.11279 12.12794 12.14290
## [673] 12.15757 12.17185 12.18562 12.19880 12.21126 12.22293 12.23368 12.24341
## [681] 12.25203 12.25995 12.26765 12.27514 12.28242 12.28951 12.29641 12.30312
## [689] 12.30966 12.31603 12.32224 12.32829 12.33419 12.33996 12.34558 12.35108
## [697] 12.35646 12.36172 12.36688 12.37194 12.37690 12.38178 12.38654 12.39117
## [705] 12.39565 12.39999 12.40420 12.40826 12.41217 12.41595 12.41958 12.42307
## [713] 12.42641 12.42961 12.43266 12.43557 12.43833 12.44095 12.44341 12.44574
## [721] 12.44791 12.44993 12.45181 12.45354 12.45512 12.45656 12.45785 12.45900
## [729] 12.46001 12.46087 12.46158 12.46215 12.46257 12.46285 12.46298 12.46296
## [737] 12.46280 12.46249 12.46203 12.46142 12.46067 12.45977 12.45872 12.45753
## [745] 12.45619 12.45472 12.45311 12.45137 12.44949 12.44748 12.44532 12.44303
## [753] 12.44061 12.43804 12.43533 12.43249 12.42950 12.42637 12.42310 12.41969
## [761] 12.41614 12.41244 12.40860 12.40462 12.40049
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")